//
//  YCCustomShareView.m
//  YCSchool
//
//  Created by Yanglixia on 16/5/10.
//  Copyright © 2016年 yanglinxia. All rights reserved.
//  分享

#import "YCCustomShareView.h"
#import <objc/message.h>


@implementation YCCustomShareCell

+ (BOOL)requiresConstraintBasedLayout
{
    return YES;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    if(self = [super initWithFrame:frame])
    {
        _imageView = [[UIImageView alloc]init];
        _imageView.layer.cornerRadius = 5;
        _imageView.clipsToBounds = YES;
        [self.contentView addSubview:_imageView];
        
        _label = [[UILabel alloc]init];
        _label.font = [UIFont systemFontOfSize:13];
        _label.textAlignment = NSTextAlignmentCenter;
        [self.contentView addSubview:_label];
        
        [_imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.height.equalTo(@60);
            make.centerX.equalTo(self.contentView);
            make.top.equalTo(@5);
        }];
        
        [_label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_imageView.mas_bottom).offset(8);
            make.centerX.equalTo(self.contentView);
        }];
    }
    return self;
}

- (void)updateConstraints
{
    [super updateConstraints];
    if(!self.updatedConstraints)
    {
        _updatedConstraints = YES;
    }
}

@end

NSString * const YCCustomShareItemImageKey = @"icon";
NSString * const YCCustomShareItemTitleKey = @"title";

@interface YCCustomShareView ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>

@property (nonatomic,readonly) UIView *maskView;
@property (nonatomic,readonly) UICollectionView *collectionView;
@property (nonatomic,readonly) UIButton *cancelButton;
@property (nonatomic,readonly) NSArray *itemArray;
@property (nonatomic,copy) YCCustomShareViewCancelBlock cancel;
@property (nonatomic,copy) YCCustomShareViewSendBlock send;

@end

@implementation YCCustomShareView

@synthesize collectionView = _collectionView;
@synthesize cancelButton = _cancelButton;

#pragma mark - 属性获取

- (UICollectionView *)collectionView
{
    if (!_collectionView)
    {
        CGFloat width = (kScreenWidth-20-30*4)/3;
        
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.scrollDirection = UICollectionViewScrollDirectionVertical;
        layout.minimumInteritemSpacing = 30;
        layout.minimumLineSpacing = 10;
        layout.itemSize = CGSizeMake(width, 100);
        layout.sectionInset = UIEdgeInsetsMake(15, 30, 15, 30);
        
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.layer.cornerRadius = 10;
        _collectionView.clipsToBounds = YES;
        [_collectionView registerClass:[YCCustomShareCell class] forCellWithReuseIdentifier:@"Cell"];
    }
    return _collectionView;
}

- (UIButton *)cancelButton
{
    if (!_cancelButton)
    {
        _cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _cancelButton.layer.cornerRadius = 10;
        _cancelButton.clipsToBounds = YES;
        [_cancelButton setTitle:@"取消" forState:UIControlStateNormal];
        _cancelButton.backgroundColor = [UIColor whiteColor];
        _cancelButton.titleLabel.font = [UIFont systemFontOfSize:15];
        [_cancelButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_cancelButton addTarget:self action:@selector(didTouchUpInsideCancelButton:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _cancelButton;
}


- (instancetype)initWithItems:(NSArray *)items
{
    CGRect frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
    if(self = [super initWithFrame:frame])
    {
        _itemArray = items;
        
        self.tintColor = [UIColor blackColor];
        self.dynamic = NO;
        self.blurRadius = 5;
        
        _maskView = [[UIView alloc] initWithFrame:self.bounds];
        _maskView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
        _maskView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.65];
        
        UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTapFromGestureRecognizer:)];
        [_maskView addGestureRecognizer:tapGestureRecognizer];
        
        [self addSubview:_maskView];
        
        [self addSubview:self.cancelButton];
        [self addSubview:self.collectionView];
        
        [_cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(@(-10));
            make.left.equalTo(@10);
            make.right.equalTo(@(-10));
            make.height.equalTo(@35);
        }];
        
        [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.equalTo(@130);
            make.left.equalTo(@10);
            make.right.equalTo(@(-10));
            make.centerX.equalTo(self);
            make.bottom.equalTo(_cancelButton.mas_top).offset(-10);
        }];
    }
    return self;
}

#pragma mark - UICollectionView datasource

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.itemArray.count;
}

#pragma mark - UICollectionView delegate

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    YCCustomShareCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
    
    cell.imageView.image = [UIImage imageNamed:self.itemArray[indexPath.row][YCCustomShareItemImageKey]];
    cell.label.text = self.itemArray[indexPath.row][YCCustomShareItemTitleKey];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if(self.send)
    {
        NSDictionary *item = self.itemArray[indexPath.row];
        self.send(self,item);
        [self.cancelButton sendActionsForControlEvents:UIControlEventTouchUpInside];
    }
}

- (void)showWithSendBlock:(YCCustomShareViewSendBlock)block cancel:(YCCustomShareViewCancelBlock)cancel
{
    UIView *view = [[UIApplication sharedApplication] keyWindow].rootViewController.view;
    
    self.cancel = cancel;
    self.send = block;
    
    [view addSubview:self];
}

- (void)didTapFromGestureRecognizer:(UITapGestureRecognizer *)recognizer
{
    [self.cancelButton sendActionsForControlEvents:UIControlEventTouchUpInside];
}

- (void)didTouchUpInsideCancelButton:(UIButton *)cancelButton
{
    if(self.cancel) self.cancel();
    [self removeFromSuperview];
}

@end
